import { defineStore } from "pinia";
import { getInfo, getUserAuth } from "../service/user/";
import jsCookie from "js-cookie";

// 创建搜索模块store
export const useUserStore = defineStore({
  id: "user",
  state: () => ({
    userInfo: {},
    auth: {},
    click:0,
  }),
  getters: {},
  actions: {
    async getUserInfo() {
      return new Promise(async (resolve, reject) => {
        // 没有token将userInfo还原
        if (!jsCookie.get("token")) {
          this.userInfo = {}
          resolve(true)
          return false
        };
        // 请求用户信息
        const userInfo = await getInfo();
        // 将用户绑定地址储存在cookie中
        jsCookie.set("address", JSON.stringify(userInfo.user.parkAreaIds))

        // 保存用户id
        jsCookie.set("userID", userInfo.user.userId)
        // 保存用户信息
        this.userInfo = userInfo.user;
        resolve(true)
      })
    },

    async updateUserAuth() {
      const result = await getUserAuth()
      this.auth = result.data
    }
  }
});
